﻿using System.IO;
using System.Linq;

namespace AgileDevTool.Helpers
{
    public class FileHelper
    {
        public static void CopyFolder(string sourceFolder, string destFolder)
        {
            CopyFolder(sourceFolder, destFolder, false);
        }

        public static void CopyFolder(string sourceFolder, string destFolder, bool cover)
        {
            CopyFolder(sourceFolder, destFolder, cover, null);
        }

        public static void CopyFolder(string sourceFolder, string destFolder, bool cover, params string[] excepts)
        {
            if (!Directory.Exists(destFolder))
            {
                Directory.CreateDirectory(destFolder);
            }
            else
            {
                if (cover)
                {
                    Directory.Delete(CacheHelper.CurrentProject.Dist, true);
                }
            }

            var folders = Directory.GetDirectories(sourceFolder);
            foreach (string folder in folders)
            {
                if (excepts!=null && excepts.Length>0)
                {
                    if (excepts.Any(a => $"{sourceFolder}{a}".ToLower() == folder.ToLower()))
                    {
                        continue;
                    }
                }
                var folderName = Path.GetFileName(folder);
                var destfolder = Path.Combine(destFolder, folderName);
                CopyFolder(folder, destfolder);
            }

            var files = Directory.GetFiles(sourceFolder);
            foreach (string file in files)
            {
                if (excepts!=null && excepts.Length>0)
                {
                    if (excepts.Any(a => $"{sourceFolder}{a}".ToLower() == file.ToLower()))
                    {
                        continue;
                    }
                }
                var name = Path.GetFileName(file);
                var dest = Path.Combine(destFolder, name);
                File.Copy(file, dest, true);
            }
        }
    }
}
